//
//  MusicModel.m
//  FuncFanc
//
//  Created by careate-hubing on 2021/8/26.
//  Copyright © 2021 huhu. All rights reserved.
//

#import "MusicModel.h"

@implementation MusicModel


+(NSArray *)loadMusicFromSandBox
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray  *localPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDirectory1 =  [[localPaths lastObject] stringByAppendingPathComponent:@"sound"];
    NSArray *path2 = [[NSBundle mainBundle]pathsForResourcesOfType:@"flac" inDirectory:@"sound/flac"];
    NSArray *paht3 = [[NSBundle mainBundle]pathsForResourcesOfType:@"mp3" inDirectory:@"sound/mp3"];
    NSArray *path1 = [[NSBundle mainBundle]pathsForResourcesOfType:@"flac" inDirectory:@"sound/Adele - Live at The Royal Albert Hal"];
    NSMutableArray *array = [NSMutableArray arrayWithArray:paht3];
    [array addObjectsFromArray:path2];
    [array addObjectsFromArray:path1];
    NSMutableArray *musicList = [NSMutableArray array];
    for (NSString *path in array) {
        NSString *allName = [path componentsSeparatedByString:@"/"].lastObject;
        NSString *typeStr = [path componentsSeparatedByString:@"."].lastObject;
        //不存在文件夹，创建文件夹。
        if (![fileManager fileExistsAtPath:docDirectory1]) {
            [fileManager createDirectoryAtPath:docDirectory1 withIntermediateDirectories:YES attributes:nil error:nil];
        }
        NSError *copyErr = nil;
        //不存在文件，复制文件过去。
        NSString *musicPath = [docDirectory1 stringByAppendingPathComponent:allName];
        if (![fileManager fileExistsAtPath:musicPath]) {
            [fileManager copyItemAtPath:path toPath:musicPath error:&copyErr];
            if (copyErr) {
                DebugLog(@"copy错误：%@",copyErr.description);
            }
        }
        MusicModel *model = [[MusicModel alloc]init];
        model.musicPath = musicPath;
        model.audioCompressionFormat = typeStr;
        model.musicName = allName;
        [musicList addObject:model];
    }
    return musicList.copy;
}

@end
